package szwolaiyun.paireach.com.szwolaizhuang.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Path;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.paireach.szwolaiyun.sz_util.SharedPreferencesUtil;
import com.paireach.szwolaiyun.sz_util.ToastUtil;
import com.zbar.lib.CaptureActivity;

import szwolaiyun.paireach.com.szwolaizhuang.R;
import szwolaiyun.paireach.com.szwolaizhuang.activity.ScanResultActivity;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.Operator;

public class HomeFragment extends Fragment implements View.OnClickListener {
    private View view;
    /**
     * 开始扫码按钮
     */
    private Button start_work;
    private TextView main_userphone, main_username;

    private String phoneNumber, name;

    public static HomeFragment getInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        phoneNumber = SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.USER_NAME);
        name = SharedPreferencesUtil.getValue(getActivity(), SharedPreferencesUtil.REAL_NAME);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null);
        initControl();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        main_userphone.setText(phoneNumber);
        main_username.setText(name);
    }

    private void initControl() {
        start_work = (Button) view.findViewById(R.id.start_work);
        start_work.setOnClickListener(this);
        main_userphone = (TextView) view.findViewById(R.id.main_userphone);
        main_username = (TextView) view.findViewById(R.id.main_username);
    }

    //扫描时间间隔设置30s
    long time = 0;//开始扫描时间

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_work:
                //30s
                if (System.currentTimeMillis() - time > 30000) {
                    startActivityForResult(new Intent(getActivity(), CaptureActivity.class), 1);
                    time = System.currentTimeMillis();
                } else {
                    ToastUtil.showShortToast(getContext(), "您的两次扫描操作小于30秒，请稍后再试");
                }
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            String result = data.getExtras().getString("_data");
            startActivity(new Intent(getActivity(), ScanResultActivity.class).putExtra("result", result));
        }
    }
}
